<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="en">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-32x32.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="C,PHP," />





  <link rel="alternate" href="/atom.xml" title="JOE'S BLOG" type="application/atom+xml" />






<meta name="description" content="zvalPHP在内核中是通过zval这个结构体来存储变量的，PHP的变量是弱类型的，首先来看看PHP5的zval设计    PHP5的zval12345678910/* 考虑到结构体对齐 大小为24字节*/struct _zval_struct&amp;#123;	/*变量信息*/	zvalue_value value;     // 16字节	zend_uint refcount__gc;  // 4字">
<meta name="keywords" content="C,PHP">
<meta property="og:type" content="article">
<meta property="og:title" content="PHP的基本变量">
<meta property="og:url" content="https://joewt.com/2018/09/11/php-extension-2.1/index.html">
<meta property="og:site_name" content="JOE&#39;S BLOG">
<meta property="og:description" content="zvalPHP在内核中是通过zval这个结构体来存储变量的，PHP的变量是弱类型的，首先来看看PHP5的zval设计    PHP5的zval12345678910/* 考虑到结构体对齐 大小为24字节*/struct _zval_struct&amp;#123;	/*变量信息*/	zvalue_value value;     // 16字节	zend_uint refcount__gc;  // 4字">
<meta property="og:locale" content="en">
<meta property="og:updated_time" content="2018-10-17T15:19:13.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="PHP的基本变量">
<meta name="twitter:description" content="zvalPHP在内核中是通过zval这个结构体来存储变量的，PHP的变量是弱类型的，首先来看看PHP5的zval设计    PHP5的zval12345678910/* 考虑到结构体对齐 大小为24字节*/struct _zval_struct&amp;#123;	/*变量信息*/	zvalue_value value;     // 16字节	zend_uint refcount__gc;  // 4字">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://joewt.com/2018/09/11/php-extension-2.1/"/>





  <title>PHP的基本变量 | JOE'S BLOG</title>
  








</head>



<body itemscope itemtype="http://schema.org/WebPage" lang="en">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">JOE'S BLOG</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">好记性不如烂键盘</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-book">
          <a href="/book/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br />
            
            book
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://joewt.com/2018/09/11/php-extension-2.1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="JOE">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/assets/img/favicon1.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JOE'S BLOG">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">PHP的基本变量</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-09-11T00:08:43+08:00">
                2018-09-11
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/PHP扩展开发/" itemprop="url" rel="index">
                    <span itemprop="name">PHP扩展开发</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/09/11/php-extension-2.1/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count gitment-comments-count" data-xid="/2018/09/11/php-extension-2.1/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">Words count in article&#58;</span>
                
                <span title="Words count in article">
                  
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">Reading time &asymp;</span>
                
                <span title="Reading time">
                  
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="zval"><a href="#zval" class="headerlink" title="zval"></a>zval</h2><p>PHP在内核中是通过zval这个结构体来存储变量的，PHP的变量是弱类型的，首先来看看PHP5的zval设计   </p>
<h3 id="PHP5的zval"><a href="#PHP5的zval" class="headerlink" title="PHP5的zval"></a>PHP5的zval</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line"> 考虑到结构体对齐 大小为24字节</span><br><span class="line">*/</span><br><span class="line">struct _zval_struct&#123;</span><br><span class="line">	/*变量信息*/</span><br><span class="line">	zvalue_value value;     // 16字节</span><br><span class="line">	zend_uint refcount__gc;  // 4字节</span><br><span class="line">	zend_uchar type;         /* 类型   1字节*/</span><br><span class="line">	zend_uchar is_ref__gc;   // 1字节</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line"> 以下说的大小是在 x86-64下</span><br><span class="line"> 在内存对齐的情况下_zvalue_value的大小为16字节</span><br><span class="line"></span><br><span class="line">*/</span><br><span class="line">typedef union _zvalue_value &#123;</span><br><span class="line">	long lval;    // 8字节</span><br><span class="line">	double dval;  // 8字节</span><br><span class="line">	struct&#123;</span><br><span class="line">		char *val;</span><br><span class="line">		int len;</span><br><span class="line">	&#125; str;        // 12字节</span><br><span class="line">	HashTable *ht;       /*HashTable 数组  8字节*/</span><br><span class="line">	zend_object_value obj;   // 12字节</span><br><span class="line">	zend_ast *ast;     // 8字节</span><br><span class="line">&#125;zvalue_value;</span><br></pre></td></tr></table></figure>
<p>PHP5的zval核心由一个zvalue_value类型的联合体和zend_uchar类型的type组成，<br><code>refcount__gc</code>是用来进行垃圾回收的，<code>is_ref__gc</code> 表示是否是引用类型<br>PHP5.3 为了解决循环引用实际申请的结构体为<code>_zval_gc_info</code>  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line">该大小为 32字节</span><br><span class="line">*/</span><br><span class="line">typedef struct _zval_gc_info &#123;</span><br><span class="line">	zval z;</span><br><span class="line">	union &#123;</span><br><span class="line">		gc_root_buffer     *buffered;</span><br><span class="line">		struct _zval_gc_info *next;</span><br><span class="line">	&#125; u;    // 4字节</span><br><span class="line">&#125;zval_gc_info;</span><br></pre></td></tr></table></figure>
<p>zval_gc_info 在内存池中分配，内存池会为每个zval_gc_info额外申请一个大小为16字节的zend_mm_block结构体，用来存放内存相关信息</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">typedef struct _zend_mm_block_info &#123;</span><br><span class="line">	size_t _size;</span><br><span class="line">	size_t _prev;</span><br><span class="line">&#125; zend_mm_block_info;</span><br><span class="line"></span><br><span class="line">typedef struct _zend_mm_block&#123;</span><br><span class="line">	zend_mm_block_info info;</span><br><span class="line">&#125; zend_mm_block;</span><br></pre></td></tr></table></figure>
<p>最终一个变量在PHP5中占48字节  </p>
<h3 id="PHP7的zval"><a href="#PHP7的zval" class="headerlink" title="PHP7的zval"></a>PHP7的zval</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">typedef union _zend_value &#123;</span><br><span class="line">	zend_long         lval;             /* 整形 */</span><br><span class="line">	double            dval;             /* 浮点型 */</span><br><span class="line">	zend_refcounted  *counted;          /* 引用计数 */</span><br><span class="line">	zend_string      *str;              /* 字符串类型 */</span><br><span class="line">	zend_array       *arr;              /* 数组类型 */</span><br><span class="line">	zend_object      *obj;              /* 对象类型 */</span><br><span class="line">	zend_resource    *res;              /* 资源类型 *</span><br><span class="line">	zend_reference   *ref;              /* 引用类型 */</span><br><span class="line">	zend_ast_ref     *ast;              /* 抽象语法树 */</span><br><span class="line">	zval             *zv;               /* zval类型 */</span><br><span class="line">	void             *ptr;              /* 指针类型 */</span><br><span class="line">	zend_class_entry *ce;               /* class类型 */</span><br><span class="line">	zend_function    *func;             /* function类型 */</span><br><span class="line">	struct &#123;</span><br><span class="line">		uint32_t w1;</span><br><span class="line">		uint32_t w2;</span><br><span class="line">	&#125; ww;</span><br><span class="line">&#125; zend_value;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">struct _zval_struct &#123;</span><br><span class="line">	zend_value        value;			/* value */</span><br><span class="line">	union &#123;</span><br><span class="line">		struct &#123;</span><br><span class="line">			ZEND_ENDIAN_LOHI_4(</span><br><span class="line">				zend_uchar    type,			/* zval类型 */</span><br><span class="line">				zend_uchar    type_flags,</span><br><span class="line">				zend_uchar    const_flags,</span><br><span class="line">				zend_uchar    reserved)	    /* call info for EX(This) */</span><br><span class="line">		&#125; v;</span><br><span class="line">		uint32_t type_info;</span><br><span class="line">	&#125; u1;</span><br><span class="line">	union &#123;</span><br><span class="line">		uint32_t     next;                 /* hash collision chain */</span><br><span class="line">		uint32_t     cache_slot;           /* literal cache slot */</span><br><span class="line">		uint32_t     lineno;               /* line number (for ast nodes) */</span><br><span class="line">		uint32_t     num_args;             /* arguments number for EX(This) */</span><br><span class="line">		uint32_t     fe_pos;               /* foreach position */</span><br><span class="line">		uint32_t     fe_iter_idx;          /* foreach iterator index */</span><br><span class="line">		uint32_t     access_flags;         /* class constant access flags */</span><br><span class="line">		uint32_t     property_guard;       /* single property guard */</span><br><span class="line">		uint32_t     extra;                /* not further specified */</span><br><span class="line">	&#125; u2;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>可以看到PHP7中zval定义的好像比较复杂但都是联合体，所以实际占用大小只有16字节<br>接下类看看各个字段的含义。  </p>
<p>u1字段的含义  </p>
<ol>
<li>type: 记录变量类型  </li>
<li>type_flag: 对应变量类型的特有的标记  </li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">IS_TYPE_CONSTANT        /* 是常量类型 */</span><br><span class="line">IS_TYPE_IMMUTABLE       /* 不可变的类型 */</span><br><span class="line">IS_TYPE_REFCOUNTED      /* 需要引用计数的类型 */</span><br><span class="line">IS_TYPE_COLLECTABLE     /* 可能包含循环引用的类型(IS_ARRAY, IS_OBJECT) */</span><br><span class="line">IS_TYPE_COPYABLE        /* 可被复制的类型 */</span><br></pre></td></tr></table></figure>
<ol>
<li>const_flag: 常量类型的标记</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">#define IS_CONSTANT_UNQUALIFIED    0x010</span><br><span class="line">#define IS_CONSTANT_VISITED_MARK   0x020</span><br><span class="line">#define IS_CONSTANT_CLASS          0x080</span><br><span class="line">#define IS_CONSTANT_IN_NAMESPACE   0x100</span><br></pre></td></tr></table></figure>
<ol>
<li>reserved: 保留字段</li>
</ol>
<p>u2字段的含义，主要是辅助字段  </p>
<ol>
<li>next: 用于哈希冲突中记录下一个元素的位置</li>
<li>cache_slot: 运行时缓存</li>
<li>lineno:  文件执行的行号，应用在AST节点上</li>
<li>num_args: 函数调用时传入参数的个数</li>
<li>fe_pos:  遍历数组时的当前位置</li>
<li>fe_iter_idx: 与fe_pos类似，针对对象使用</li>
<li>access_flags: 对象类的访问标志, public protected private</li>
<li>property_guard: 防止类中魔术方法的循环调用</li>
</ol>
<h2 id="PHP7-变量类型"><a href="#PHP7-变量类型" class="headerlink" title="PHP7 变量类型"></a>PHP7 变量类型</h2><p>PHP7 通过定义20种宏，来u1.v.type字段</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">/* regular data types */</span><br><span class="line">#define IS_UNDEF                    0</span><br><span class="line">#define IS_NULL                     1</span><br><span class="line">#define IS_FALSE                    2</span><br><span class="line">#define IS_TRUE                     3</span><br><span class="line">#define IS_LONG                     4</span><br><span class="line">#define IS_DOUBLE                   5</span><br><span class="line">#define IS_STRING                   6</span><br><span class="line">#define IS_ARRAY                    7</span><br><span class="line">#define IS_OBJECT                   8</span><br><span class="line">#define IS_RESOURCE                 9</span><br><span class="line">#define IS_REFERENCE                10</span><br><span class="line"></span><br><span class="line">/* constant expressions */</span><br><span class="line">#define IS_CONSTANT                 11</span><br><span class="line">#define IS_CONSTANT_AST             12</span><br><span class="line"></span><br><span class="line">/* fake types */</span><br><span class="line">#define _IS_BOOL                    13</span><br><span class="line">#define IS_CALLABLE                 14</span><br><span class="line">#define IS_ITERABLE                 19</span><br><span class="line">#define IS_VOID                     18</span><br><span class="line"></span><br><span class="line">/* internal types */</span><br><span class="line">#define IS_INDIRECT                 15  /* 间接类型 */</span><br><span class="line">#define IS_PTR                      17  /* 指针类型 */</span><br><span class="line">#define _IS_ERROR                   20  /* 错误类型 *</span><br></pre></td></tr></table></figure>
<h3 id="整形和浮点型"><a href="#整形和浮点型" class="headerlink" title="整形和浮点型"></a>整形和浮点型</h3><p>整形和浮点型在zval是直接存储的，实现的比较简单</p>
<h3 id="字符串类型"><a href="#字符串类型" class="headerlink" title="字符串类型"></a>字符串类型</h3><p>字符串类型的结构如下共占32字节  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">struct _zend_string&#123;</span><br><span class="line">	zend_refcounted_h gc;     // 用于引用计数 8字节</span><br><span class="line">	zend_ulong        h;      // 哈希值 8字节</span><br><span class="line">	size_t            len;    // 字符串长度 8字节</span><br><span class="line">	char              val[1]; // 柔性数组，1字节 字符串的值存储位置</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">typedef struct _zend_refcounted_h &#123;</span><br><span class="line">    uint32_t        refcounted; </span><br><span class="line">    union &#123;</span><br><span class="line">        struct&#123;</span><br><span class="line">            ZEND_ENDIAN_LOHI_3 (</span><br><span class="line">                zend_uchar  type,</span><br><span class="line">                zend_uchar  flags,</span><br><span class="line">                uint16_t    gc_info</span><br><span class="line">            )</span><br><span class="line">    &#125; v;</span><br><span class="line">    uint32_t type_info;</span><br><span class="line">    &#125; u;</span><br><span class="line">&#125;zend_refcounted_h;</span><br><span class="line">```  </span><br><span class="line">#### 字段说明  </span><br><span class="line">gc   </span><br><span class="line">gc字段主要存放引用计数等信息。flags存放类别信息，type存放变量类别  </span><br><span class="line"></span><br><span class="line">h  </span><br><span class="line">h字段的作用是缓存字符串的哈希值  </span><br><span class="line"></span><br><span class="line">val  </span><br><span class="line">val字段存储字符串值  </span><br><span class="line"></span><br><span class="line">len  </span><br><span class="line">字符串长度  </span><br><span class="line"></span><br><span class="line">PHP中的字符串是二进制安全的，在遇到\0的时候并不认为结束了，读取的时候以len的值为准  </span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#### 几个常用字符串的操作  </span><br><span class="line">echo 输出一个或多个字符串  </span><br><span class="line">在Zend/zend_vm_execute.h下</span><br></pre></td></tr></table></figure>
<p>static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ECHO_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)<br>{<br>    USE_OPLINE</p>
<pre><code>zval *z;

SAVE_OPLINE();
z = EX_CONSTANT(opline-&gt;op1); //获取变量

if (Z_TYPE_P(z) == IS_STRING) { // 判断是否是字符串 是的或就直接输出
    zend_string *str = Z_STR_P(z); 

    if (ZSTR_LEN(str) != 0) {
        zend_write(ZSTR_VAL(str), ZSTR_LEN(str));  //将字符串写入到标准输出
    }
} else {
    zend_string *str = _zval_get_string_func(z);  //强制转换

    if (ZSTR_LEN(str) != 0) {
        zend_write(ZSTR_VAL(str), ZSTR_LEN(str));   //写入到标准输出
    } else if (IS_CONST == IS_CV &amp;&amp; UNEXPECTED(Z_TYPE_P(z) == IS_UNDEF)) {
        GET_OP1_UNDEF_CV(z, BP_VAR_R);
    }
    zend_string_release(str);
}

ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
</code></pre><p>}</p>
<p>```</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>感谢土豪</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>Donate</span>
  </button>
  <div id="QR" style="display: none;">

    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/assets/img/alipay.png" alt="JOE Alipay"/>
        <p>Alipay</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>Post author:</strong>
    JOE
  </li>
  <li class="post-copyright-link">
    <strong>Post link:</strong>
    <a href="https://joewt.com/2018/09/11/php-extension-2.1/" title="PHP的基本变量">https://joewt.com/2018/09/11/php-extension-2.1/</a>
  </li>
  <li class="post-copyright-license">
    <strong>Copyright Notice: </strong>
    All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> unless stating additionally.
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/C/" rel="tag"># C</a>
          
            <a href="/tags/PHP/" rel="tag"># PHP</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/09/01/php-extension-1-1/" rel="next" title="认识PHP的一些基本原理">
                <i class="fa fa-chevron-left"></i> 认识PHP的一些基本原理
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/24/c-advanced/" rel="prev" title="C语言中几个基本概念">
                C语言中几个基本概念 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      
        <div id="gitment-container"></div>
      
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/assets/img/favicon1.png"
                alt="JOE" />
            
              <p class="site-author-name" itemprop="name">JOE</p>
              <p class="site-description motion-element" itemprop="description">爱生活，爱coding</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">9</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/yinrenxin" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:joewttx@163.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://learnerwn.github.io" title="Murphy" target="_blank">Murphy</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://strcpy.me" title="virusdefender" target="_blank">virusdefender</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://qulongjun.com/" title="落苏" target="_blank">落苏</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://tysb7.cn/" title="汤圆" target="_blank">汤圆</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://aconion.com" title="阿葱" target="_blank">阿葱</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://csy-anxuanxun.cn" title="csy" target="_blank">csy</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://hakana.cn" title="幻梦" target="_blank">幻梦</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.fastrun.cn" title="CrazyCodes" target="_blank">CrazyCodes</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.heanny.cn" title="Heanny" target="_blank">Heanny</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://vkkevin.xyz" title="Kevin" target="_blank">Kevin</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#zval"><span class="nav-number">1.</span> <span class="nav-text">zval</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP5的zval"><span class="nav-number">1.1.</span> <span class="nav-text">PHP5的zval</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP7的zval"><span class="nav-number">1.2.</span> <span class="nav-text">PHP7的zval</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#PHP7-变量类型"><span class="nav-number">2.</span> <span class="nav-text">PHP7 变量类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#整形和浮点型"><span class="nav-number">2.1.</span> <span class="nav-text">整形和浮点型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字符串类型"><span class="nav-number">2.2.</span> <span class="nav-text">字符串类型</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">JOE</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>








        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  








  







<!-- LOCAL: You can save these files to your site and update links -->
    
        
        <link rel="stylesheet" href="https://billts.site/extra_css/gitment.css">
        <script src="https://billts.site/js/gitment.js"></script>
    
<!-- END LOCAL -->

    

    
      <script type="text/javascript">
      function renderGitment(){
        var gitment = new Gitmint({
            id: window.location.pathname, 
            owner: 'Joewt',
            repo: 'blogComment',
            
            lang: "" || navigator.language || navigator.systemLanguage || navigator.userLanguage,
            
            oauth: {
            
            
                client_secret: 'aeba4f8f14a7d3464ef3ea9faef0d67409229e47',
            
                client_id: '12908903defa4c3347b8'
            }});
        gitment.render('gitment-container');
      }

      
      renderGitment();
      
      </script>
    







  





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?0c58a1486de42ac6cc1c59c7d98ae887"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/koharu.model.json"},"display":{"position":"left","width":75,"height":150},"mobile":{"show":true},"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/"});</script></body>
</html>
